import json
from contextvars import ContextVar, copy_context
from datetime import timedelta, datetime, timezone
from zoneinfo import ZoneInfo
import asyncio
import jwt
from pydantic import BaseModel

from src.utils.common_utils import hash_password

# access_token_expires = timedelta(minutes=10)
# print(access_token_expires)
# expire = datetime.now(tz=ZoneInfo("Asia/Shanghai")) + access_token_expires
# print(expire)
#
# to_encode = {"user_id": 1, "username": "admin", "exp": expire}
# print('原始数据', to_encode)
# token = jwt.encode(to_encode, '123456', algorithm='HS512')
# print('编码', token)
#
# payload = jwt.decode(token, '123456', algorithms=['HS512'])
# print('解码', type(payload))
# username = payload.get("sub")
# expire = payload.get("exp")
# print('过期时间', expire)
# print(username, datetime.fromtimestamp(expire, tz=ZoneInfo("Asia/Shanghai")))


# print(int(datetime.now(tz=ZoneInfo("Asia/Shanghai")).timestamp()))


# # 添加输出日志的功能
# def logging(flag):
#     def decorator(fn):
#         def inner(num1, num2):
#             if flag == "+":
#                 print("--正在努力加法计算--")
#             elif flag == "-":
#                 print("--正在努力减法计算--")
#             result = fn(num1, num2)
#             return result
#
#         return inner
#
#     # 返回装饰器
#     return decorator
#
#
# # 使用装饰器装饰函数
# @logging("+")
# def add(a, b):
#     result = a + b
#     return result
#
#
# print(add(1, 8))

# print(hash_password("123456"))


# ctx = ContextVar('ctx', default=42)  # 创建一个上下文变量，name=ctx，默认值=42
#
# print('----> 获取默认值 ：', ctx.get())
# # 通过ctx.set()设置ctx的值
# token = ctx.set(100)
# # 通过ctx.get()获取ctx的值
# print('----> 获取ctx.set()设置ctx的值 ：', ctx.get())
# token_2 = ctx.set(300)
# print('----> 获取ctx.set()设置ctx的值 ：', ctx.get())
# # 通过ctx.reset()重置ctx的值，恢复到设置token操作之前的值
# ctx.reset(token_2)
#
# print('----> ctx.reset()重置ctx的值 ：',ctx.get())


var = ContextVar('var')
var.set('spam')
print('第一次获取值 ： ', var.get())


def var_handler():
    # 'var' was set to 'spam' before
    # calling 'copy_context()' and 'ctx.run(main)', so:
    print(f"before var.get() =={var.get()} ，ctx[var] == ={ctx[var]}", )

    var.set('ham')

    # Now, after setting 'var' to 'ham':
    print(f"after var.get() =={var.get()} ，ctx[var] == ={ctx[var]}", )


# 赋值当前上下文中所有状态，生成一个独立的 Context 对象
ctx = copy_context()

# Any changes that the 'main' function makes to 'var'
# will be contained in 'ctx'.
# 在新生成的ctx上下文中运行，与 主上下文（var）隔离运行
ctx.run(var_handler)

# The 'main()' function was run in the 'ctx' context,
# so changes to 'var' are contained in it:
# 又回到主上下文中运行
print(f"not in var_handler, var.get() =={var.get()} , ctx[var] =={ctx[var]}")

# ctx2 = ContextVar('ctx2')
#
#
# async def ctx2_get():
#     print(f'Request ID (Inner)=={ctx2.get()}')
#
#
# async def ctx2_set(value):
#     ctx2.set(value)
#     await ctx2_get()  # 设置完后去获取ctx
#     print(f'Request ID (Outer)=={ctx2.get()}')
#
#
# async def ctx2_handler():
#     tasks = []
#     for value in range(1, 5):
#         tasks.append(asyncio.create_task(ctx2_set(value)))
#     await asyncio.gather(*tasks)
#
#
# asyncio.run(ctx2_handler())
mm = '1,2,3,4,5'.split(',')
print( mm)

dd = [1,2,3,4,5]
print(', '.join(map(str, dd)))

import uuid
print(uuid.uuid4())
